package com.wtwd.wtwddevice.listener.appMsg;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.CmdStateEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.model.BloodOxygenWarning;
import com.wtwd.device.model.HeartRateWarning;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.SetBloodOxygenWarning;
import com.wtwd.wtwddevice.model.entity.SetHeartRateWarning;
import com.wtwd.wtwddevice.mq.PushAppMsgService;
import com.wtwd.wtwddevice.service.IDeviceSendService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.ISetBloodOxygenWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@MsgType(MsgTypeEnum.APP_SET_BLOOD_OXYGEN_WARNING)
@Component
public class AppSetBloodOxygenWarning implements IMsgHandler {
    @Resource
    private PushAppMsgService msgService;

    @Resource
    private IDeviceSendService deviceSendService;

    @Resource
    private IDeviceService deviceService;

    @Autowired
    private ISetBloodOxygenWarningService setBloodOxygenWarningService;

    @Override
    public void handleMsg(MessageBase messageBase) {
        AppProtocol appProtocol = messageBase.getAppProtocol();
        String number = deviceSendService.generateNumber(appProtocol.getImei());
        Long deviceId = deviceService.getDeviceIdByImei(appProtocol.getImei());
        JSONObject json = (JSONObject) appProtocol.getData();
        BloodOxygenWarning data = json.toJavaObject(BloodOxygenWarning.class);
        String body = data.serialization();
        WTProtocol wtProtocol = WTProtocol.builder()
                .imei(appProtocol.getImei())
                .cmdName(MsgTypeEnum.SET_OX_THRESHOLD.getCmdName())
                .body(body)
                .bodyLength(body.length())
                .number(number)
                .build();
        if (deviceSendService.sendMsgDevice(wtProtocol)){//如果为fase直接告知app端设置失败

            SetBloodOxygenWarning setBloodOxygenWarning = new SetBloodOxygenWarning();

            setBloodOxygenWarning.setDeviceId(deviceId);
            setBloodOxygenWarning.setMaxBloodOxygen(data.getMaxBloodOxygen());
            setBloodOxygenWarning.setMinBloodOxygen(data.getMinBloodOxygen());
            setBloodOxygenWarning.setNumber(number);
            setBloodOxygenWarning.setTime(wtProtocol.getTime());
            setBloodOxygenWarning.setCreateTime(System.currentTimeMillis());
            setBloodOxygenWarning.setUpdateTime(System.currentTimeMillis());
            setBloodOxygenWarning.setState(CmdStateEnum.SENDING.getState());

            setBloodOxygenWarningService.editSetBloodOxygenWarning(setBloodOxygenWarning);
        }else {//
            AppProtocol protocol = AppProtocol.builder()
                    .imei(wtProtocol.getImei())
                    .cmdName(MsgTypeEnum.APP_SET_HEART_RATE_WARNING.getCmdName())
                    .state(AnswerStateEnum.ABNORMAL.getState())
                    .data(null)
                    .build();
            msgService.pushToDevice(messageBase.getForm(),
                    MessageBase.builder()
                            .appProtocol(protocol)
                            .build()
            );
        }
    }
}
